<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>bindKey 示例</title>
    <style>
        /* 页面整体样式 */
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        /* 页面容器样式 */
        .container {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 400px;
            text-align: center;
        }

        /* 标题样式 */
        h1 {
            color: #333;
            font-size: 24px;
        }

        /* 按钮样式 */
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #45a049;
        }

        /* 结果显示样式 */
        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f0f0f0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            color: #555;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>bindKey 函数示例</h1>
        <p>点击按钮调用绑定函数并显示结果</p>
        <button id="callFunctionBtn">调用绑定函数</button>
        <div class="result" id="result">结果将在此显示</div>
    </div>

    <script>
        // bindKey 函数定义
        const bindKey = (context, handler, ...bindArgs) => (...args) =>
            context[handler].apply(context, [...bindArgs, ...args]);

        // 创建一个对象，包含一个处理函数
        const person = {
            name: '小明',
            greet(greeting, punctuation) {
                return `${greeting}，${this.name}${punctuation}`;
            }
        };

        // 绑定 greet 函数，传入固定的问候语和标点符号
        const boundGreet = bindKey(person, 'greet', '你好', '！');

        // 事件处理函数，当点击按钮时调用绑定的函数
        document.getElementById('callFunctionBtn').addEventListener('click', () => {
            const result = boundGreet();  // 调用绑定的 greet 函数
            document.getElementById('result').textContent = result;  // 显示结果
        });
    </script>
</body>

</html>